<?php
class admin_contentModel extends baseModel{
    protected $table = 'content_data';

    //内容列表
    public function content_list($limit=null,$where=null,$order=null){
        
        $sql="
            SELECT A.*,B.name as cname,B.mid
            FROM {$this->model->pre}content A 
            LEFT JOIN {$this->model->pre}category B ON A.cid = B.cid
            WHERE B.name<>'' {$where} ORDER BY {$order}A.updatetime DESC,A.aid DESC LIMIT {$limit}
            ";
        $data=$this->model->query($sql);
        return $data;
    }

    //获取内容统计
	public function count($where=null) {

        $sql="
            SELECT count(*) as num
            FROM {$this->model->pre}content A 
            LEFT JOIN {$this->model->pre}category B ON A.cid = B.cid
            WHERE B.name<>'' {$where}
            ";
        $data=$this->model->query($sql);
        return $data[0]['num'];
	}

    //公共信息处理
    public function common_data_save($data)
    {

        //自动分页
        $data['tool_page']=intval($data['tool_page']);
        $data['tool_page_num']=intval($data['tool_page_num']);
        if($data['tool_page']&&$data['tool_page_num']&&$data['content']){
            $content=html_out($data['content']);
            $page_tag='<hr class="ke-pagebreak" />';
            $content=str_replace($page_tag,'', $content);
            $content_list=html_explode($content,$data['tool_page_num'],$page_tag);
            if(!empty($content_list)){
                $content='';
                foreach ($content_list as $value) {
                   $content.=$value. "\n".$page_tag;
                }
            }
            $data['content']=substr($content, 0,-27);
        }
        //格式化部分字段
        $data['content']=html_in($data['content']);
        return $data;
    }


    //内容表信息
    public function add_save($data)
    {
        $data=$this->common_data_save($data);
        return $this->insert($data);
    }

    //获取附加内容
    public function info($aid)
    {
        return $this->find('aid='.$aid);
    }


    //内容表信息
    public function edit_save($data)
    {
        $data=$this->common_data_save($data);
        return $this->update('aid='.$data['aid'], $data);
    }


    public function del($aid)
    {
        return $this->model->table('content_data')->where('aid='.$aid)->delete(); 
    }

    

}

?>